home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / sunbird / js / calTransactionManager.js < prev    next >
Encoding:
JavaScript  |  2007-05-23  |  7.9 KB  |  223 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Calendar Transaction Manager code.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  *   Philipp Kewisch (mozilla@kewis.ch)
  18.  * Portions created by the Initial Developer are Copyright (C) 2006
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37. function calTransactionManager() {
  38.     if (!this.transactionManager) {
  39.         this.transactionManager =
  40.             Components.classes["@mozilla.org/transactionmanager;1"]
  41.                       .createInstance(Components.interfaces.nsITransactionManager);
  42.     }
  43. }
  44.  
  45. calTransactionManager.prototype = {
  46.  
  47.     QueryInterface: function cTM_QueryInterface(aIID) {
  48.         if (!aIID.equals(Components.interfaces.nsISupports) &&
  49.             !aIID.equals(Components.interfaces.calITransactionManager))
  50.         {
  51.             throw Components.results.NS_ERROR_NO_INTERFACE;
  52.         }
  53.         return this;
  54.     },
  55.  
  56.     transactionManager: null,
  57.  
  58.     createAndCommitTxn: function cTM_createAndCommitTxn(aAction,
  59.                                                         aItem,
  60.                                                         aCalendar,
  61.                                                         aOldItem,
  62.                                                         aListener) {
  63.         var txn = new calTransaction(aAction,
  64.                                      aItem,
  65.                                      aCalendar,
  66.                                      aOldItem,
  67.                                      aListener);
  68.         this.transactionManager.doTransaction(txn);
  69.     },
  70.  
  71.     beginBatch: function cTM_beginBatch() {
  72.         this.transactionManager.beginBatch();
  73.     },
  74.  
  75.     endBatch: function cTM_endBatch() {
  76.         this.transactionManager.endBatch();
  77.     },
  78.  
  79.     undo: function cTM_undo() {
  80.         this.transactionManager.undoTransaction();
  81.     },
  82.  
  83.     canUndo: function cTM_canUndo() {
  84.         return (this.transactionManager.numberOfUndoItems > 0);
  85.     },
  86.  
  87.     redo: function cTM_redo() {
  88.         this.transactionManager.redoTransaction();
  89.     },
  90.  
  91.     canRedo: function cTM_canRedo() {
  92.         return (this.transactionManager.numberOfRedoItems > 0);
  93.     }
  94. };
  95.  
  96. function calTransaction(aAction, aItem, aCalendar, aOldItem, aListener) {
  97.     this.mAction = aAction;
  98.     this.mItem = aItem;
  99.     this.mCalendar = aCalendar;
  100.     this.mOldItem = aOldItem;
  101.     this.mListener = aListener;
  102. }
  103.  
  104. calTransaction.prototype = {
  105.  
  106.     mAction: null,
  107.     mCalendar: null,
  108.     mItem: null,
  109.     mOldItem: null,
  110.     mOldCalendar: null,
  111.     mListener: null,
  112.     mIsDoTransaction: false,
  113.  
  114.     QueryInterface: function cT_QueryInterface(aIID) {
  115.         if (!aIID.equals(Components.interfaces.nsISupports) &&
  116.             !aIID.equals(Components.interfaces.nsITransaction) &&
  117.             !aIID.equals(Components.interfaces.calIOperationListener))
  118.         {
  119.             throw Components.results.NS_ERROR_NO_INTERFACE;
  120.         }
  121.         return this;
  122.     },
  123.  
  124.     onOperationComplete: function cT_onOperationComplete(aCalendar,
  125.                                                          aStatus,
  126.                                                          aOperationType,
  127.                                                          aId,
  128.                                                          aDetail) {
  129.         if (aStatus == Components.results.NS_OK &&
  130.             (aOperationType == Components.interfaces.calIOperationListener.ADD ||
  131.              aOperationType == Components.interfaces.calIOperationListener.MODIFY)) {
  132.             if (this.mIsDoTransaction) {
  133.                 this.mItem = aDetail;
  134.             } else {
  135.                 this.mOldItem = aDetail;
  136.             }
  137.         }
  138.         if (this.mListener) {
  139.             this.mListener.onOperationComplete(aCalendar,
  140.                                               aStatus,
  141.                                               aOperationType,
  142.                                               aId,
  143.                                               aDetail);
  144.         }
  145.     },
  146.  
  147.     onGetResult: function cT_onGetResult(aCalendar,
  148.                                          aStatus,
  149.                                          aItemType,
  150.                                          aDetail,
  151.                                          aCount,
  152.                                          aItems) {
  153.         if (this.mListener) {
  154.             this.mListener.onGetResult(aCalendar,
  155.                                        aStatus,
  156.                                        aItemType,
  157.                                        aDetail,
  158.                                        aCount,
  159.                                        aItems);
  160.         }
  161.     },
  162.  
  163.     doTransaction: function cT_doTransaction() {
  164.         this.mIsDoTransaction = true;
  165.         switch (this.mAction) {
  166.             case 'add':
  167.                 this.mCalendar.addItem(this.mItem, this);
  168.                 break;
  169.             case 'modify':
  170.                 this.mCalendar.modifyItem(this.mItem,
  171.                                           this.mOldItem,
  172.                                           this);
  173.                 break;
  174.             case 'delete':
  175.                 this.mCalendar.deleteItem(this.mItem, this);
  176.                 break;
  177.             case 'move':
  178.                 this.mOldCalendar = this.mOldItem.calendar;
  179.                 this.mOldCalendar.deleteItem(this.mOldItem, this);
  180.                 this.mCalendar.addItem(this.mItem, this);
  181.                 break;
  182.             default:
  183.                 throw new Components.Exception("Invalid action specified",
  184.                                                Components.results.NS_ERROR_ILLEGAL_VALUE);
  185.                 break;
  186.         }
  187.     },
  188.     
  189.     undoTransaction: function cT_undoTransaction() {
  190.         this.mIsDoTransaction = false;
  191.         switch (this.mAction) {
  192.             case 'add':
  193.                 this.mCalendar.deleteItem(this.mItem, this);
  194.                 break;
  195.             case 'modify':
  196.                 this.mCalendar.modifyItem(this.mOldItem, this.mItem, this);
  197.                 break;
  198.             case 'delete':
  199.                 this.mCalendar.addItem(this.mItem, this);
  200.                 break;
  201.             case 'move':
  202.                 this.mCalendar.deleteItem(this.mItem, this);
  203.                 this.mOldCalendar.addItem(this.mOldItem, this);
  204.                 break;
  205.             default:
  206.                 throw new Components.Exception("Invalid action specified",
  207.                                                Components.results.NS_ERROR_ILLEGAL_VALUE);
  208.                 break;
  209.         }
  210.     },
  211.     
  212.     redoTransaction: function cT_redoTransaction() {
  213.         this.doTransaction();
  214.     },
  215.  
  216.     isTransient: false,
  217.  
  218.     merge: function cT_merge(aTransaction) {
  219.         // No support for merging
  220.         return false;
  221.     }
  222. };
  223.